package com.bkgy.controller.orderInfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.orderInfo.entity.TL3HotcoilEnterStore;
import com.bkgy.controller.orderInfo.entity.TOrderState;
import com.bkgy.controller.orderInfo.entity.TOrderState2;
import com.bkgy.controller.orderInfo.entity.pdo.OrderInfoRep;
import com.bkgy.controller.orderInfo.entity.pdo.OverOrderInfoRep;
import com.bkgy.controller.orderInfo.mapper.ITOrderStateMapper;
import com.bkgy.controller.orderInfo.service.ITOrderStateService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
@Slf4j
@RequiredArgsConstructor
public class TOrderStateServiceImpl extends ServiceImpl<ITOrderStateMapper, TOrderState> implements ITOrderStateService {
    private final ITOrderStateMapper mapper;

    @Override
    public ResultData<OrderInfoRep> orderRep() {
        //查询最近3个月的订单数据
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -2);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String startDate = DateUtil.getDateToString(cal.getTime(), "yyyyMM");

        int month = 4;//最近三个月的数据，第一个位置用于合计
        Map<String, TOrderState[]> map = new HashMap<>();
        List<String> orderNoList = new ArrayList<>();
        for (int i = 1; i < month; i++) {
            List<TOrderState> listMonth = mapper.selectOrderInfo(startDate);
            for (TOrderState orderState : listMonth) {
                orderNoList.add(orderState.getOrderNo().substring(0, orderState.getOrderNo().length() - 2));
                if (orderState.getClientName().startsWith("广汽传祺")) orderState.setClientName("广汽传祺");

                String key = orderState.getOrderTypes() + ";" + orderState.getSufaceClass() + ";" + orderState.getClientName();
                if (!map.containsKey(key)) {
                    TOrderState[] arr = new TOrderState[4];
                    arr[i] = orderState;
                    map.put(key, arr);
                } else {
                    TOrderState oldState = map.get(key)[i];
                    if (oldState != null) {
                        oldState.setAnnealStock(oldState.getAnnealStock().add(orderState.getAnnealStock()));
                        oldState.setRecoilingStock(oldState.getRecoilingStock().add(orderState.getRecoilingStock()));
                        oldState.setWaitSend(oldState.getWaitSend().add(orderState.getWaitSend()));
                        oldState.setOrderWeight(oldState.getOrderWeight().add(orderState.getOrderWeight()));
                        oldState.setLessWeight(oldState.getLessWeight().add(orderState.getLessWeight()));
                        oldState.setALessWeight(oldState.getALessWeight().add(orderState.getALessWeight()));
                        oldState.getAlterList().addAll(orderState.getAlterList());
                    } else {
                        map.get(key)[i] = orderState;
                    }
                }
            }

            //将时间往后退一个月
            cal.add(Calendar.MONTH, +1);
            startDate = DateUtil.getDateToString(cal.getTime(), "yyyyMM");
        }

        List<OrderInfoRep> resList = new ArrayList<>();
        if(orderNoList.isEmpty()){
            log.warn("订单信息不存在");
            return ResultData.ResultDataSuccess(resList, 0);
        }
        Map<String, BigDecimal> matActMap = new HashMap<>();
        List<TL3HotcoilEnterStore> matActWtList = mapper.selectActWt(orderNoList);
        for (TL3HotcoilEnterStore matActWt : matActWtList) {
            matActMap.put(matActWt.getOrderNo(), matActWt.getMatActWt());
        }

        //获得一个自定义顺序的key列表
        List<String> keyList = orderKey(map.keySet());
        for (String key : keyList) {
            TOrderState[] rowArr = map.get(key);
            OrderInfoRep rep = new OrderInfoRep();
            //0位置用于合计
            for (int i = 1; i < rowArr.length; i++) {
                //某月可能没有数据
                if (rowArr[i] != null) {
                    TOrderState orderState = rowArr[i];
                    rep.setOrderTypes(orderState.getOrderTypes());
                    rep.setSufaceClass(orderState.getSufaceClass());
                    rep.setClientName(orderState.getClientName());
                    //给对应的月设值
                    BigDecimal sumYuanliao = BigDecimal.ZERO;
                    String orderNo = orderState.getOrderNo().substring(0, orderState.getOrderNo().length() - 2);
                    if (matActMap.containsKey(orderNo)) {
                        sumYuanliao = matActMap.get(orderNo);
                    }
                    //待过=LESS_WEIGHT-原料库
                    BigDecimal daiguo = orderState.getLessWeight().subtract(sumYuanliao);
                    BigDecimal zhahou = orderState.getAnnealStock();
                    BigDecimal jingzheng = orderState.getRecoilingStock().add(orderState.getALessWeight());
                    BigDecimal dinghuo = orderState.getOrderWeight();
                    BigDecimal zhunfa = orderState.getALessWeight();

                    //给对应的月份赋值
                    try {
                        rep.getClass().getMethod("setMon" + i + 0, BigDecimal.class).invoke(rep, daiguo);
                        rep.getClass().getMethod("setMon" + i + 1, BigDecimal.class).invoke(rep, sumYuanliao);
                        rep.getClass().getMethod("setMon" + i + 2, BigDecimal.class).invoke(rep, zhahou);
                        rep.getClass().getMethod("setMon" + i + 3, BigDecimal.class).invoke(rep, jingzheng);
                        rep.getClass().getMethod("setMon" + i + 4, BigDecimal.class).invoke(rep, dinghuo);
                        rep.getClass().getMethod("setMon" + i + 5, BigDecimal.class).invoke(rep, zhunfa);
                        rep.getClass().getMethod("setMon" + i + "List", List.class).invoke(rep, orderState.getAlterList());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    rep.setSum0(rep.getSum0().add(daiguo));
                    rep.setSum1(rep.getSum1().add(sumYuanliao));
                    rep.setSum2(rep.getSum2().add(zhahou));
                    rep.setSum3(rep.getSum3().add(jingzheng));
                    rep.setSum4(rep.getSum4().add(dinghuo));
                    rep.setSum5(rep.getSum5().add(zhunfa));

                }
            }
            resList.add(rep);
        }
        return ResultData.ResultDataSuccess(resList, resList.size());
    }

    @Override
    public ResultData<OverOrderInfoRep> overOrderRep() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -3);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String startDate = DateUtil.getDateToString(cal.getTime(), "yyyyMM");

        //查询显示4个月的数据
        int monthTotal = 4;
        Map<String, TOrderState2[]> map = new HashMap<>();
        for (int i = 0; i < monthTotal; i++) {
            List<TOrderState2> listMonth1 = mapper.selectOverOrderInfo(startDate);
            for (TOrderState2 orderState : listMonth1) {
                //转换产线名称
                String key = orderState.getOrderTypes();
                if (key.indexOf("冷轧") != -1) key = "三冷连退线";
                if (key.indexOf("热镀锌") != -1) key = "三冷镀锌线";

                if (!map.containsKey(key)) {
                    TOrderState2[] arr = new TOrderState2[monthTotal];
                    arr[i] = orderState;
                    map.put(key, arr);
                } else {
                    //合并成一个对象
                    TOrderState2 old = map.get(key)[i];
                    if (old != null) {
                        old.setLessWeight(old.getLessWeight().add(orderState.getLessWeight()));
                        old.setAnnealStock(old.getAnnealStock().add(orderState.getAnnealStock()));
                        old.setRecoilingStock(old.getRecoilingStock().add(orderState.getRecoilingStock()));
                        old.setWaitSend(old.getWaitSend().add(orderState.getWaitSend()));
                        old.setALessWeight(old.getALessWeight().add(orderState.getALessWeight()));
                    } else map.get(key)[i] = orderState;
                }
            }
            //将月份往后退1个月
            cal.add(Calendar.MONTH, +1);
            startDate = DateUtil.getDateToString(cal.getTime(), "yyyy/MM");
        }

        List<OverOrderInfoRep> resList = new ArrayList<>();
        for (Map.Entry<String, TOrderState2[]> entry : map.entrySet()) {
            TOrderState2[] rowArr = entry.getValue();
            OverOrderInfoRep rep = new OverOrderInfoRep();
            for (int i = 0; i < rowArr.length; i++) {
                //某月可能没有数据
                if (rowArr[i] != null) {
                    TOrderState2 orderState = rowArr[i];
                    rep.setLastLine(entry.getKey());

                    //给对应的月份赋值
                    try {
                        BigDecimal jingzheng = orderState.getRecoilingStock().add(orderState.getWaitSend());
                        rep.getClass().getMethod("setMon" + i + 0, BigDecimal.class).invoke(rep, orderState.getLessWeight());
                        rep.getClass().getMethod("setMon" + i + 2, BigDecimal.class).invoke(rep, orderState.getAnnealStock());
                        rep.getClass().getMethod("setMon" + i + 3, BigDecimal.class).invoke(rep, jingzheng);
                        rep.getClass().getMethod("setMon" + i + 4, BigDecimal.class).invoke(rep, orderState.getALessWeight());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            resList.add(rep);
        }
        return ResultData.ResultDataSuccess(resList, resList.size());
    }

    private List<String> orderKey(Set<String> kesSet) {
        List<String> keyList = new ArrayList<>();
        for (String key : kesSet) {
            if (key.startsWith("冷轧钢卷")) keyList.add(1 + key);
            else if (key.startsWith("冷轧纵切")) keyList.add(2 + key);
            else if (key.startsWith("热镀锌钢")) keyList.add(3 + key);
            else if (key.startsWith("酸洗平整")) keyList.add(4 + key);
            else keyList.add(5 + key);
        }
        Collections.sort(keyList);
        List<String> resList = new ArrayList<>();
        for (String key : keyList) {
            resList.add(key.substring(1));
        }

        return resList;
    }
}
